package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwagT;
import cn.e4j.modular.crm.entity.GwaglT;
import cn.e4j.modular.crm.mapper.GwagTMapper;
import cn.e4j.modular.crm.service.GwagTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  行业二级分类 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-10
 */
@Service
public class GwagTServiceImpl extends ServiceImpl<GwagTMapper, GwagT> implements GwagTService {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaglist(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwagDtos = this.baseMapper.listGwagPage(page, dto);

        return gwagDtos;
    }

    @Transactional(readOnly = true)
    public GwagT getByGwag001(String ent,String gwag001) {
        GwagT gwag = this.baseMapper.getByGwag001(ent, gwag001);
        return gwag;
    }

    @Transactional
    @Override
    public void add(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwagT gwag = new GwagT();
        gwag.setGWAG001(dto.getString("GWAG001"));
        gwag.setGWAGENT(shiroUser.getDBC());
        gwag.setGWAGACTI(dto.getString("GWAGACTI"));
        gwag.setGWAG002(dto.getString("GWAG002"));
        gwag.setGWAG003(" ");//预留栏位2
        gwag.setGWAGUSER(shiroUser.getACCOUNT());
        gwag.setGWAGGRUP(shiroUser.getORG_ID());
        gwag.setGWAGDATE(LocalDate.now());
        gwag.setGWAGTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwag.setGWAGORIG(shiroUser.getORG_ID());
        gwag.setGWAGORIU(shiroUser.getACCOUNT());
        GwaglT gwagl = new GwaglT();
        gwagl.setGWAGLENT(shiroUser.getDBC());
        gwagl.setGWAGL001(dto.getString("GWAG001"));
        gwagl.setGWAGL002(LangUtils.getLang(shiroUser.getLANG()));
        gwagl.setGWAGL003(dto.getString("GWAGL003"));
        gwagl.setGWAGL004(dto.getString("GWAGL004"));
        gwag.insert();
        gwagl.insert();
    }

    @Transactional
    @Override
    public void edit(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwagT gwag = new GwagT();
        gwag.setGWAGACTI(dto.getString("GWAGACTI"));
        gwag.setGWAG002(dto.getString("GWAG002"));
        gwag.setGWAGMODU(shiroUser.getACCOUNT());
        UpdateWrapper<GwagT> gwagTUpdateWrapper = new UpdateWrapper<>();
        gwagTUpdateWrapper.eq("GWAGENT",shiroUser.getDBC());
        gwagTUpdateWrapper.eq("GWAG001",dto.getString("GWAG001"));

        GwaglT gwagl = new GwaglT();
        gwagl.setGWAGL003(dto.getString("GWAGL003"));
        gwagl.setGWAGL004(dto.getString("GWAGL004"));
        UpdateWrapper<GwaglT> gwaglTUpdateWrapper = new UpdateWrapper<>();
        gwaglTUpdateWrapper.eq("GWAGLENT",shiroUser.getDBC());
        gwaglTUpdateWrapper.eq("GWAGL001",dto.getString("GWAG001"));

        gwag.update(gwagTUpdateWrapper);
        gwagl.update(gwaglTUpdateWrapper);
    }


    @Transactional
    @Override
    public void delete(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwagT gwag = new GwagT();
        UpdateWrapper<GwagT> gwagTUpdateWrapper = new UpdateWrapper<>();
        gwagTUpdateWrapper.eq("GWAGENT",shiroUser.getDBC());
        gwagTUpdateWrapper.eq("GWAG001",dto.getString("GWAG001"));

        GwaglT gwagl = new GwaglT();
        UpdateWrapper<GwaglT> gwaglTUpdateWrapper = new UpdateWrapper<>();
        gwaglTUpdateWrapper.eq("GWAGLENT",shiroUser.getDBC());
        gwaglTUpdateWrapper.eq("GWAGL001",dto.getString("GWAG001"));

        gwag.delete(gwagTUpdateWrapper);
        gwagl.delete(gwaglTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwagT> gwagTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for(GwagT gwagT : gwagTList){
            UpdateWrapper<GwagT> gwagTUpdateWrapper = new UpdateWrapper<>();
            gwagTUpdateWrapper.eq("GWAGENT",shiroUser.getDBC());
            gwagTUpdateWrapper.eq("GWAG001",gwagT.getGWAG001());

            GwaglT gwagl = new GwaglT();
            UpdateWrapper<GwaglT> gwaglTUpdateWrapper = new UpdateWrapper<>();
            gwaglTUpdateWrapper.eq("GWAGLENT",shiroUser.getDBC());
            gwaglTUpdateWrapper.eq("GWAGL001",gwagT.getGWAG001());

            gwagT.delete(gwagTUpdateWrapper);
            gwagl.delete(gwaglTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwbwList(Dto dto) {

        List<Map<String, Object>> gwbwDtos = this.baseMapper.getGwbwList(dto);

        return gwbwDtos;
    }
}
